<template>
  <view class="chat-tools">
    <template v-for="(item, index) in chatToolsList">
      <view class="chat-tool" v-if="item.user" :key="item.title" @click="handleToolsClick(item)">
        <text>{{ item.title || "我是按钮" }}</text>
      </view>
    </template>
  </view>
</template>

<script>
import { getVasRights, getProductGroup } from "@/api/modules/career";
import { addConsultantRate, getRateTypeList } from "@/api/modules/career.js";
export default {
  name: "chatTools",
  props: {
    bookTime: {
      type: String,
      defalut: "",
    },
    isConsultant: {
      type: Boolean,
      default: false,
    },
    isZhituUser: {
      type: Boolean,
      default: false,
    },
    //是否存在有效聊天
    hansRealChat: {
      type: Boolean,
      default: false,
    },
    chatId: {
      type: Number,
      required: true,
    },
    personId: {
      type: Number,
      required: true,
    },
    selfInfo: {
      type: Object,
      required: true,
    },
    consultantInfo: {
      type: Object,
      required: true,
    },
    chatList: {
      type: Array,
      required: false,
    },
  },
  watch: {},
  data() {
    return {
      projectName: "",
      isBuy: 0, // 是否购买
      chatToolsList: [
        {
          title: "购买服务",
          icon: "https://img.yzcdn.cn/vant/cat.jpeg",
          user: this.isZhituUser,
        },
        {
          title: "常见问题",
          icon: "https://img.yzcdn.cn/vant/cat.jpeg",
          user: this.isZhituUser,
        },
        {
          title: "评价顾问",
          icon: "https://img.yzcdn.cn/vant/cat.jpeg",
          user: this.isZhituUser,
          outText: "顾问评价",
        },
        {
          title: "预约顾问",
          icon: "",
          user: this.isZhituUser,
          outText: "预约顾问",
        },
        {
          title: "发送评价",
          icon: "https://img.yzcdn.cn/vant/cat.jpeg",
          user: this.isConsultant,
          outText: "顾问评价",
        },
      ],
      rateList: [],
      isRateSend: false,
    };
  },
  created() {
    this.reqGetRateTypeList();
    // this.getVasRights();
    this.getProductGroup();
  },
  methods: {
    async getProductGroup() {
      const params = {
        personId: uni.getStorageSync("person-id").slice(2) || undefined,
        id: "1",
      };
      const res = await getProductGroup(params);
      if (res.code == 200 && Array.isArray(res.data) && res.data.length) {
        this.projectName = res.data[0].productSign;
      }
    },
    async getVasRights() {
      const params = {
        cardType: uni.getStorageSync("cardType"),
        personId: this.isConsultant ? this.chatId : this.personId,
      };
      const res = await getVasRights(params);
      if (res.code == 200) {
        if (res.data) {
          if (res.data.CONSULTE_SERVICE) {
            this.isBuy = res.data.CONSULTE_SERVICE.isBuy;
            if (
              res.data.CONSULTE_SERVICE.experience[0].leftValue == 0 &&
              res.data.CONSULTE_SERVICE.isBuy == 0
            ) {
              uni.showToast({ icon: "none", title: "请先购买服务~" });
            } else {
              console.log("aaaaaaaa");
              if (!this.isConsultant) {
                this.$emit("showPopupContent");
              }
            }
          } else {
            this.$emit("showPopupContent");
          }
        }
      }
    },
    reqGetRateTypeList() {
      getRateTypeList().then((res) => {
        if (res.code == 200) {
          this.rateList = res.data;
        } else {
          uni.showToast({
            title: "服务器开小差了",
            icon: "none",
            duration: 1000
          });
        }
      });
    },
    handleToolsClick(item) {
      let rateInfo = {};
      if (this.isConsultant) {
        rateInfo = {
          consultantName:
            this.selfInfo.info.nickname || uni.getStorageSync("personName"),
          consultantId: this.selfInfo.username,
          userPersonId: this.chatId,
        };
      } else {
        rateInfo = {
          consultantName: this.consultantInfo.iname,
          consultantId: this.chatId || uni.getStorageSync("consultantId"),
          userPersonId: this.personId,
        };
      }

      if (item.title == "发送评价" || item.title == "评价顾问") {
        console.log("这里点击了发送评价");
        if (this.isRateSend) {
          uni.showToast({
            title: "已经发送过评价了",
            icon: "none",
            duration: 1000
          });
          return;
        }
        if (!this.rateList.length) {
          uni.showToast({
            title: "获取评分维度失败",
            icon: "none",
            duration: 1000
          });
          return;
        }
        if (this.isZhituUser && !this.hansRealChat) {
          uni.showToast({
            title: "还未与顾问咨询过，不能评价顾问~",
            icon: "none",
            duration: 1000
          });
          return;
        }

        //评价顾问要判断时间是否已经过了预约的开始时间
        // let bookDate = new Date(this.bookTime.slice(0, 16)); // 预约时间

        // let nowDate = new Date(); // 当前时间
        // //当当前时间小于预约时间时，return
        // if (bookDate.getTime() > nowDate.getTime()) {
        //   uni.showToast({
        //     title: "还未与顾问咨询过，不能评价顾问~",
        //     icon: "none",
        //   });
        //   return;
        // }
        addConsultantRate({
          //后端只能存bigInt所以和后端交互要去掉后缀
          consultantId: rateInfo.consultantId.replace("_consultant", ""),
          personId: rateInfo.userPersonId,
          chatRoles: this.isConsultant ? "10" : "20",
        }).then((res) => {
          if (res.code == 200) {
            const text = "本次咨询服务已完成，请为我评分！";
            var idate = new Date().getTime();
            let msg = {
              send_uid: this.isConsultant
                ? this.selfInfo.username
                : this.chatId,
              receive_uid: this.isConsultant
                ? this.chatId
                : this.selfInfo.username,
              content: text,
              person_iname: this.selfInfo.info.nickname,
              person_pic: this.selfInfo.info.extras.GET_PIC,
              is_send: "1",
              idate: idate,
              type: 1,
            };
            var content = {
              send_uid: this.selfInfo.username,
              receive_uid: this.chatId,
              content: (item.outText || item.title) + "",
              share: {
                type: 204,
                rateInfo,
                rateId: res.data.id,
                rateList: this.rateList,
              },
              person_iname: this.selfInfo.info.nickname,
              person_pic: this.selfInfo.info.extras.GET_PIC,
              is_send: "1",
              idate: new Date().getTime(),
            };

            //发送提醒消息后，延迟发送评价消息

            if (this.isConsultant) {
              this.$emit("createMsgContent", msg);
            }
            setTimeout(() => {
              this.$emit("createMsgContent", content);
            }, 300);
            this.isRateSend = true;
          } else {
            uni.showToast({
              title: "服务器开小差了",
              icon: "none",
              duration: 1000
            });
          }
        });
      } else if (item.title === "预约顾问") {
        let bookDate = new Date(this.bookTime.slice(0, 16)); // 预约时间
        let nowDate = new Date(); // 当前时间
        // 获取两个时间的时间戳（单位为毫秒）
        let bookDatestamp = bookDate.getTime();
        let nowDatestamp = nowDate.getTime();
        if (bookDatestamp > nowDatestamp) {
          console.log("此时时间没到");
          uni.showToast({
            icon: "none",
            title: "你已预约过该顾问~",
            duration: 1000
          });
        } else if (nowDatestamp <= bookDatestamp + 1800000) {
          console.log("时间到了");
          uni.showToast({
            icon: "none",
            title: "咨询正在进行中，暂不可预约~",
            duration: 1000
          });
        } else {
          // 在这里判断是否是体验版 如果不是体验版走正常的流程 如果是体验版就要判断预约次数
          this.getVasRights();
          // this.$emit("showPopupContent");
        }
      } else {
        if (item.title != "购买服务") {
          this.$emit("setSystemMsg", "您可能想咨询以下内容？", true);
        }
      }
      if (item.title == "购买服务") {
        console.log("这里点击了购买服务", this.projectName);
        switch (this.projectName) {
          case "CONSULTE_SERVICE":
            uni.navigateTo({
              url: `/pagesSub/userCenter/careerPathV2/components/BuyPage`,
            });
            break;
          case "ZHITU_WUHAN_SERVICE":
            uni.navigateTo({
              url: `/pagesSub/userCenter/careerPathV2/components/WuHanBuyPage`,
            });
            break;
          case "ZHITU_TOC_SERVICE":
            uni.navigateTo({
              url: `/pagesSub/userCenter/careerPathV2/components/CBuyPage`,
            });
            break;
          default:
            uni.showToast({
              title: "当前服务已购买，无需重复购买~",
              icon: "none",
              duration: 1000
            });
            break;
        }

        // this.isBuy = uni.getStorageSync("isBuy");
        // let editionType = uni.getStorageSync("edition-Type");
        // console.log("this.isBuy", this.isBuy);
        // if (this.isBuy || editionType == 10) {
        //   uni.showToast({
        //     title: "当前服务已购买，无需重复购买~",
        //     icon: "none",
        //   });
        // } else {
        //   uni.navigateTo({
        //     url: `/pagesSub/userCenter/careerPathV2/components/BuyPage`,
        //   });
        // }
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.chat-tools {
  display: flex;
  background-color: #f7f8fa;
  padding-bottom: 14rpx;
  padding-left: 20rpx;

  .chat-tool {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10rpx 20rpx;
    background: #fcfcfc;
    border-radius: 30rpx;
    font-size: 30rpx;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    color: #333333;
    margin-right: 24rpx;
  }
}
</style>
